package com.xsir.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xsir.common.Result;
import com.xsir.entity.User;
import com.xsir.service.IArticleService;
import com.xsir.service.IUserService;
import com.xsir.util.Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author xsir
 * @date 2020/9/5
 * <p>
 * 公共控制层
 * 功能：
 * - 首页跳转
 * - 注册
 * - 登录
 */
@Controller
@Slf4j
public class CommonController {
    @Autowired
    private IUserService IUserService;
    @Autowired
    private IArticleService IArticleService;
    
    /**
     * 未登录
     * @return
     */
    @RequestMapping("/notLogin")
    @ResponseBody
    public Result notLogin() {
        return Result.error("当前未登录！");
    }
    
    @RequestMapping("/unauthorized")
    @ResponseBody
    public Result unAuth() {
        return Result.error("权限不足！");
    }
    
    /**
     * 发送验证码
     *
     * @param email 目标邮箱
     * @return 结果
     */
    @RequestMapping("/authCode")
    @ResponseBody
    public Result sendAuthCode(@RequestParam String email) {
        if (StringUtils.isEmpty(email) || !email.matches(Util.REGEX_EMAIL)) {
            return Result.error("邮箱格式不正确！", HttpStatus.BAD_REQUEST.value());
        }
        
        // 发送验证码
        // 1、随机生成四位数的验证码
        String authCode = Util.randomCode(4);
        Boolean success = Util.sendSimpleMail(email, "注册验证码", "验证码为：" + authCode + "(5分钟后过期)");
        if (!success) {
            return Result.error("验证码发送失败", 200);
        }
        // 2、设置验证码的有效时间为5分钟
        Session session = SecurityUtils.getSubject().getSession();
        
        session.setAttribute(email, authCode);
        session.setTimeout(5 * 60 * 1000);
        return Result.ok("验证码已发送");
    }
    
    
    @GetMapping("/checkName")
    @ResponseBody
    public Result checkName(@RequestParam String username) {
        if(StringUtils.isEmpty(username)) {
            return Result.error("用户名不能为空");
        }
    
        User user = IUserService.getOne(new QueryWrapper<User>().eq("username", username));
        if(user != null) {
            return Result.error("用户名已存在");
        }
        
        return Result.ok();
    }
    
    @GetMapping("/checkEmail")
    @ResponseBody
    public Result checkEmail(@RequestParam String email) {
        if(StringUtils.isEmpty(email)) {
            return Result.error("邮箱不能为空");
        }
        User user = IUserService.getOne(new QueryWrapper<User>().eq("account", email));
        if(user != null) {
            return Result.error("邮箱已被注册");
        }
    
        return Result.ok();
    }
}
